// pages/service/service.js
var app = getApp();
var util = require('../../utils/util');
Page({

  /**
   * 页面的初始数据
   */
  data: {
    currentPage: 1
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function(options) {
    var that=this;
    this.setData({
      date: util.formatTime(new Date),
      orderlist: []
    },function(){
      that.myOrderList();
    })
    
  },
  //选择日期
  bindDateChange: function(e) {
    //   console.log('picker发送选择改变，携带值为', e.detail.value)
    this.setData({
      date: e.detail.value,
      currentPage: 1,
      orderlist: []
    })
    this.myOrderList();
  },
  //我的订单
  myOrderList: function() {
    var that = this;
    util.showLoading();
    var apiUrl = '/api/dis/order/my_order_list';
    var apiData = {
      send_time: that.data.date,
      page: that.data.currentPage
    }
    util.fetchPost(apiUrl, apiData, function(res) {
      util.hideLoading();
      if (res.state == 1) {
        var allpost = that.data.orderlist;
        if (allpost == undefined) {
          allpost = [];
        }
        for (var i in res.datas) {
          allpost.push(res.datas[i])
        }
        if (res.datas.length < 10) {
          that.setData({
            tishi_title: '已加载全部',
          });
        } else {
          that.setData({
            tishi_title: '加载更多',
          });
        }
        that.setData({
          orderlist: allpost,
          ready: res.datas.length == 0 ? 1 : 2,
        });
      } else {
        util.message(res.datas);
      }
    });
  },
  onSearchTap: function() {
    wx.navigateTo({
      url: '../searchlist/searchlist?send_time=' + this.data.date,
      success: function(res) {},
      fail: function(res) {},
      complete: function(res) {},
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function() {

  },


  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function() {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function() {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function() {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
	onPullDownRefresh: function () {
		var that = this;
		that.setData({
			orderlist: [],
			currentPage: 1
		})
		that.myOrderList()
	},
  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function() {
	  var that = this;
	  if (that.data.ready == 2) {
		  var page = that.data.currentPage + 1
		  that.setData({
			  currentPage: page,
		  })
		  that.myOrderList()
	  }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function() {

  }
})